﻿namespace CN2P.ServiceModel.ServiceClients
{
    using System.Collections.Generic;
    using System.Net;

    public class RecommendationsServiceClient : BaseServiceClient
    {
        public RecommendationsServiceClient(string baseUrl)
            : base(baseUrl)
        {
        }

        public void AddTraversal(string fromProductId, string toProductId)
        {
            var response = this.ExecutePost(_baseUrl + "api/recommendation/traversal", @"{
""from"": ""{0}"", ""to"": ""{1}""}".Replace("{0}", fromProductId).Replace("{1}", toProductId));
            this.ValidateResponse(response, HttpStatusCode.Created);
        }

        public IList<string> Get(IList<string> prodIds)
        {
            var url = _baseUrl + "api/recommendation/";
            url += "?";
            for (var i = 0; i < prodIds.Count; i++)
            {
                if (i > 0)
                {
                    url += "&";
                }

                url += "prodId=" + prodIds[i];
            }

            var response = this.ExecuteGet<IList<string>>(url);
            this.ValidateResponse(response, HttpStatusCode.OK);

            return response.Content;
        }

        public void AddTraversal(string json)
        {
            var response = this.ExecutePost(_baseUrl + "api/recommendation/traversal", json);
            this.ValidateResponse(response, HttpStatusCode.Created);
        }
        
        public void AddRating(string productId, float rating)
        {
            var response = this.ExecutePost(
                _baseUrl + "api/recommendation/rating", @"{""prodId"": ""{0}"", ""avgRating"": {1} }"
                .Replace("{0}", productId)
                .Replace("{1}", rating.ToString("F", System.Globalization.CultureInfo.GetCultureInfo("en-US"))));  // marioszp: Had to change this due to my system having german culture settings which resulted in wrong "ToString()" result for JSON serialization.
            this.ValidateResponse(response, HttpStatusCode.Created);
        }

        public void DeleteAll()
        {
            var url = _baseUrl + "api/recommendation/";
            var response = this.ExecuteDelete(url);
            this.ValidateResponse(response, HttpStatusCode.Created);
        }
    }
}
